
<style>
body {
    overflow: hidden;
    margin: 0px;
    padding: 0px;
    background: white;
}

div {
    cursor: pointer;
    text-align: left;
    padding: 2px 3px;
    font-family: sans-serif;
    font-size: 0.8em;
    width: 170px;
    margin-top: 1px;
    background: #eeeeee;
    color: #101010;
}
div:first-child {
    margin-top: 0px;
}
div:hover {
    background: #466bf3;
    color: #f3f3ff;
}

.separator{
    cursor:default;
    font-size: 0.6em;
    color: gray;
    background: white;
}
.separator:hover {
    color: gray;
    background: white;
}

#fastdial       {cursor:default; background: #bbddff; }
#fastdial:hover { color:#101010; }

#reloadtab       { background: #ddffbb; }
#reloadtab:hover { background: #466bf3; }

</style>
<script type="text/javascript" src="js/localStorage.js"></script>
<script type="text/javascript" src="js/init.js"></script>

<script type="text/javascript">

function standardized_url(url)
{
    var newurl = url;
    if(url.indexOf("http") != 0) {  newurl = "http://"+url; }
    return newurl;
}

function new_journal_tab(url) 
{
    var ezproxy = "http://libproxy1.nus.edu.sg/login?url=%@";
    var journal_url = ezproxy.replace('%@', standardized_url(url) );

    chrome.tabs.create({url: journal_url});
    window.close();
}

function reload_tab_with_lib_proxy() {
    var ezproxy = "http://libproxy1.nus.edu.sg/login?url=%@";

    chrome.tabs.getSelected(undefined, function(tab) {
	var this_tab_url = tab.url;

	if(this_tab_url.indexOf("libproxy1.nus.edu.sg") >= 0) { return; }
	if(this_tab_url.indexOf("http") != 0) { return; } 

	var new_url = ezproxy.replace('%@', this_tab_url);

        chrome.tabs.update(tab.id, {url: new_url});
    });

    window.close();
}

function retrieve_a_paper() {
    var q = (document.getElementById('j-select')).value;
    var vol = (document.getElementById('jvol')).value;
    var page = (document.getElementById('page')).value;

    /* validates 'volume' and 'page' inputs*/
    document.getElementById('vol_label').style.color = (vol =='')?"red":'black';
    document.getElementById('page_label').style.color= (page=='')?"red":'black';
    if(vol == '' || page == '') return;

    /* go Search Page */
    var paper_url = eval(q);
    chrome.tabs.create({url: paper_url});

    window.close();
}

function generate_key(type, k)
{
    var key = type + k;
    return key;
}

function populate_q()
{
    var len = Number(getItem("num_of_qs"));

    var j_name = new Array();
    var j_q    = new Array();

    for(var i = 0; i < len; i++) {
	var key_Q = generate_key("Q", i);
	var Q = getItem(key_Q).split('\t');
	j_name[i] = Q[0];
	j_q[i]    = Q[1];
    }

    var Sel = document.getElementById('j-select');
    for(var i = 0; i < len; i++) {
	var new_option = document.createElement('Option');
	new_option.value=j_q[i];
	new_option.innerHTML=j_name[i];
	Sel.appendChild(new_option);
    }
}

function populate_journal_portals()
{
    var len = Number(
		getItem("num_of_portals")
	      );

    var j_name = new Array();
    var j_urls = new Array();

    for(var i = 0; i < len; i++) {
	var key_J = generate_key("J", i);
	var J = getItem(key_J).split('\t');
	j_name[i] = J[0];
	j_urls[i] = J[1];
    }

    for(var i = 0; i < len; i++) {
	var divStr="<div onclick=\"new_journal_tab(\'"+j_urls[i]+"\')\">"
						      +j_name[i]+"</div>";
	document.write(divStr);
    }
}

init();

</script>

<!--Reload Tab Div-->
<div onclick="reload_tab_with_lib_proxy()" id="reloadtab">
    Reload this tab with the NUS Library Proxy
</div>

<!--Separator-->
<div class="separator"> 
    Journal Portal 
    <a style="color:green; float:right" href="" onclick="chrome.tabs.create({url: 'options.html'}); window.close();">Customize</a>
</div>

<!--Journal Portal Div-->
<script type="text/javascript">
    populate_journal_portals();
</script>

<!--Separator-->
<div class="separator"> 
    Fast Dial
    <a style="color:green; float:right" href="" onclick="chrome.tabs.create({url: 'options.html'}); window.close();">Customize</a>
</div>

<!--Fast Dial Div-->
<div id="fastdial">
  <form style="display:inline" onSubmit="return retrieve_a_paper()">
    <span id='vol_label' >vol </span>
    <input type="text" id="jvol" style="width:35px">
    <span id='page_label'>page </span>
    <input type="text" id="page" style="width:65px">

    <select name="journal-select" id="j-select" style="width:120px"> </select>
    <script>populate_q();</script>

    <button type="submit" id="submit-button" >Go</button>
</form>
</div>


